package cn.jksoft.businessclient.ui.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jksoft.businessclient.R;
import cn.jksoft.businessclient.bean.CustCardEntity;

/**
 * Author:  nick
 * Email:   nickdevp@gmail.com
 * Date:    16/5/5 11:33.
 * Description:
 */
public class MembershipCardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {


    private Context context;
    private LayoutInflater inflater;
    CKCardAdapter mAdapter;
    //item类型
    public static final int ITEM_TYPE_CZK = 1;//储值卡
    public static final int ITEM_TYPE_CK = 2;//次卡
    public static final int TYPE_HEADER = 0;
    public static final int TYPE_NORMAL = 1;
    private List<CustCardEntity.CustomerCardListEntity> dataList;
    private View.OnClickListener itemClickListener;
    private View.OnClickListener buttonClickListenner;
    private View mHeaderView;

    public void setItemClickListener(View.OnClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public void setButtonClickListenner(View.OnClickListener buttonClickListenner) {
        this.buttonClickListenner = buttonClickListenner;
    }

    public MembershipCardAdapter(Context context, List<CustCardEntity.CustomerCardListEntity> list) {
        this.context = context;
        this.dataList = list;
        inflater = LayoutInflater.from(context);
    }

    public void addDataList(List<CustCardEntity.CustomerCardListEntity> list) {
        this.dataList.addAll(list);
        notifyDataSetChanged();
    }

    public void setHeaderView(View headerView) {
        mHeaderView = headerView;
        notifyItemInserted(0);
    }

    public View getHeaderView() {
        return mHeaderView;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEADER;
        }else{
            if (dataList.get(position-1).getCARD_KIND() == 1) {
                return ITEM_TYPE_CZK;
            } else {
                return ITEM_TYPE_CK;
            }
        }

//        if (dataList.get(position).getCARD_KIND() == 1) {
//            return ITEM_TYPE_CZK;
//        } else {
//            return ITEM_TYPE_CK;
//        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        if(mHeaderView != null && viewType == TYPE_HEADER){
            return new Holder(mHeaderView);
        }else if (viewType == ITEM_TYPE_CZK) {
            View view = inflater.inflate(R.layout.item_membership_czk, viewGroup, false);
            return new Item1ViewHolder(view);
        } else {
            View view = inflater.inflate(R.layout.item_membership_ck, viewGroup, false);
            return new Item2ViewHolder(view);
        }
    }

    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }


    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder itemViewHolder, int i) {
        if(getItemViewType(i) == TYPE_HEADER) return;

        final int pos = getRealPosition(itemViewHolder);

        CustCardEntity.CustomerCardListEntity dataBean = dataList.get(pos);
        if (itemViewHolder instanceof Item1ViewHolder) {
            ((Item1ViewHolder) itemViewHolder).tvCardName.setText(dataBean.getCARD_NAME());
            ((Item1ViewHolder) itemViewHolder).tvExpireDate.setText(dataBean.getEXPIRE_DAY());
            ((Item1ViewHolder) itemViewHolder).tvCardBalance.setText(dataBean.getBALANCE() / 100 + "");
            ((Item1ViewHolder) itemViewHolder).itemView.setTag(pos);
            ((Item1ViewHolder) itemViewHolder).itemView.setOnClickListener(itemClickListener);
        } else if (itemViewHolder instanceof Item2ViewHolder) {
            ((Item2ViewHolder) itemViewHolder).tvCardName.setText(dataBean.getCARD_NAME());
            ((Item2ViewHolder) itemViewHolder).tvExpireDate.setText(dataBean.getEXPIRE_DAY());
            ((Item2ViewHolder) itemViewHolder).itemView.setTag(pos);
            ((Item2ViewHolder) itemViewHolder).itemView.setOnClickListener(itemClickListener);
            //设置布局管理器
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);

            ((Item2ViewHolder) itemViewHolder).mRecyclerView.setLayoutManager(linearLayoutManager);
            mAdapter = new CKCardAdapter(context, dataBean.getMemberPackageItemList());
            ((Item2ViewHolder) itemViewHolder).mRecyclerView.setAdapter(mAdapter);
            if(dataBean.getMemberPackageItemList().size()>4){
                ((Item2ViewHolder) itemViewHolder).ivArrow.setVisibility(View.VISIBLE);
            }else{
                ((Item2ViewHolder) itemViewHolder).ivArrow.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemCount() {
      //  return dataList.size();
        return mHeaderView == null ? dataList.size() : dataList.size() + 1;
    }

    public static class Item1ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_card_name)
        TextView tvCardName;
        @Bind(R.id.tv_expire_date)
        TextView tvExpireDate;
        @Bind(R.id.tv_card_balance)
        TextView tvCardBalance;

        public Item1ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public static class Item2ViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.tv_card_name)
        TextView tvCardName;
        @Bind(R.id.tv_expire_date)
        TextView tvExpireDate;
        @Bind(R.id.service_recyclerview)
        RecyclerView mRecyclerView;
        @Bind(R.id.arrow_icon)
        ImageView ivArrow;


        public Item2ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class Holder extends RecyclerView.ViewHolder {

        public Holder(View itemView) {
            super(itemView);
            if(itemView == mHeaderView) return;
        }
    }
}
